//index.js
//获取应用实例
import {request, requestD} from "../../request/request.js"
import {formatTimeMDHM, compareCellT} from "../../utils/util.js"

const app = getApp()

Page({
  data: {
    imgURLs: [
      'https://ftp.bmp.ovh/imgs/2020/04/03b8e62b86e7db18.jpg',
      'https://ftp.bmp.ovh/imgs/2020/04/4ea85ab20a797fd3.jpg'
    ],
    news:[
      // {
      //   type: 'news',
      //   id: 1,
      //   top: true,
      //   title: '社区放假安排',
      //   date: '01-02 10:53',
      //   abstract: '最新社区放假安排如下：请所有人员回家关灯睡觉。'
      // },
      // {
      //   type: 'news',
      //   id: 2,
      //   top: false,
      //   title: '36栋施工',
      //   date: '01-01 10:00',
      //   abstract: '36栋楼附近将于22:00-6:00施工，各位居民放心休息。'
      // },
      // {
      //   type: 'news',
      //   id: 3,
      //   top: false,
      //   title: '新年快乐',
      //   date: '01-01 00:00',
      //   abstract: '祝各位2020年新年新气象！'
      // }

    ]

  },


  onLoad: function (options) {
    requestD({
      url:'news/getNewsList'
    }).then(result=>{
      const resp = result.data
      console.log(resp)
      if(resp.error_code==0){
        const newsList = resp.data
        newsList.forEach((v,i)=>{
          v.type = 'news'
          v.id = v.news_id
          v.top = v.top==1?true:false
          v.date = formatTimeMDHM(new Date(v.create_time))
          v.abstract = v.content.substring(0, 20)
        })
        newsList.sort((a,b)=>compareCellT(a,b))
        this.setData({
          news:newsList
        })
      }else{
        wx.showToast({
          title: '未获取新闻',
          icon: 'none',
          duration: 800
        })
      }
    })

  }

  
})
